package me.zhengjie.controller.freeze;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.dao.FreezeDao;
import me.zhengjie.dao.MaterialDao;
import me.zhengjie.dao.WareNestDao;
import me.zhengjie.dao.dto.FreezeTaskRecordQueryCriteria;
import me.zhengjie.dao.dto.WareNestQueryCriteria;
import me.zhengjie.server.FreezeService;
import me.zhengjie.utils.IpUtils;
import me.zhengjie.utils.PageResult;
import me.zhengjie.vo.freeze.FreezeVo;
import me.zhengjie.vo.Result;
import me.zhengjie.vo.merge.AgainBindVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/freeze")
public class FreezeController {
    private static final Logger freezeTaskLog = LoggerFactory.getLogger("freeze");

    @Resource
    private FreezeService freezeService;

    @GetMapping(value = "/findWareMaterial")
    @ResponseBody
    public ResponseEntity<PageResult<WareNestDao>> findWareMaterial(@ModelAttribute WareNestQueryCriteria criteria) {
        freezeTaskLog.info("查询可冻结物料信息");
        return new ResponseEntity<>(freezeService.findWareMaterial(criteria), HttpStatus.OK);
    }

    @PostMapping(value = "/findFreezeTaskList")
    @ResponseBody
    public ResponseEntity<PageResult<FreezeDao>> findFreezeTaskList(@ModelAttribute FreezeTaskRecordQueryCriteria criteria) {
        freezeTaskLog.info("查询冻结任务信息");
        return new ResponseEntity<>(freezeService.findFreezeTaskList(criteria), HttpStatus.OK);
    }

    @PostMapping(value = "/freezeBarCodeList")
    @ResponseBody
    public Result<?> freezeBarCodeList(@RequestBody FreezeVo freezeVo, HttpServletRequest httpServletRequest) {
        freezeTaskLog.info("提交需要冻结物料");
        if (freezeVo == null) {
            return Result.error("提交数据为空");
        }
        String remoteIp = IpUtils.getRemoteIp(httpServletRequest);
        freezeVo.setWorkIp(remoteIp);
        return freezeService.freeze(freezeVo);
    }

    @GetMapping(value = "/freezeOutBound")
    @ResponseBody
    public Result<?> freezeOutBound(String taskId) {
        freezeTaskLog.info("开启任务出库载具");
        return freezeService.startTaskFindFreezeOutBound(taskId);
    }

    /**
     * 定时任务处理出库
     */
    // @Scheduled(fixedDelay = 5000)
    public void scheduledTasksOutBound() {
        freezeTaskLog.info("定时任务处理冻结出库");
        freezeService.scheduledTasksOutBound();
    }

    @GetMapping(value = "/scanFreezeContainerCode")
    @ResponseBody
    public Result<List<MaterialDao>> scanFreezeContainerCode(String containerCode) {
        freezeTaskLog.info("扫描冻结载具编码获取冻结物料信息");
        return freezeService.scanFreezeContainerCode(containerCode);
    }

    @PostMapping(value = "/rebindItemBarCodeList")
    @ResponseBody
    public Result<?> rebindItemBarCodeList(@RequestBody AgainBindVo againBindVo) {
        freezeTaskLog.info("重新绑定冻结物料与未冻结物料");
        return freezeService.rebindItemBarCodeList(againBindVo);
    }

    @GetMapping(value = "/countFreezeContainerCode")
    @ResponseBody
    public Result<Map<String, Integer>> countFreezeContainerCode(String taskId) {
        freezeTaskLog.info("获取待分拣的载具数量");
        return freezeService.countFreezeContainerCode(taskId);
    }

    @PostMapping(value = "/completeFreeze")
    @ResponseBody
    public Result<?> completeFreeze(@RequestBody Map<String, Object> params) {
        freezeTaskLog.info("载具冻结分拣完成回库");
        if (params == null) {
            return Result.error("提交数据为空");
        }
        return freezeService.completeFreeze(params);
    }

    @PostMapping(value = "/unFreeze")
    @ResponseBody
    public Result<?> unFreeze(@RequestBody FreezeVo freezeVo) {
        freezeTaskLog.info("解冻物料");
        return freezeService.unFreeze(freezeVo);
    }

}
